#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\experimental\finitedifferences\fdmquantohelper.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Experimental { namespace Finitedifferences {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IFdmQuantoHelper
	public ref class CFdmQuantoHelper : 
            public System::MarshalByRefObject,
            public Cephei::QL::Experimental::Finitedifferences::IFdmQuantoHelper
	{
	protected: 
		boost::shared_ptr<QuantLib::FdmQuantoHelper>* _ppFdmQuantoHelper;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::FdmQuantoHelper>* _phFdmQuantoHelper;
#endif
		Object^ _FdmQuantoHelperOwner;     // reference to object that manages the storage for this object
	internal:
		CFdmQuantoHelper (Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ fTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ fxVolTS, Double equityFxCorrelation, Double exchRateATMlevel);
        CFdmQuantoHelper (boost::shared_ptr<QuantLib::FdmQuantoHelper>& childNative, Object^ owner);
        CFdmQuantoHelper (QuantLib::FdmQuantoHelper& childNative, Object^ owner);
        CFdmQuantoHelper (CFdmQuantoHelper^ copy);
        CFdmQuantoHelper (System::Type^ t);
#ifdef STRUCT
        CFdmQuantoHelper (QuantLib::FdmQuantoHelper childNative);
#endif       
#ifdef HANDLE
		CFdmQuantoHelper (QuantLib::Handle<QuantLib::FdmQuantoHelper>& childNative, Object^ owner);
		CFdmQuantoHelper (QuantLib::Handle<QuantLib::FdmQuantoHelper> childNative);
#endif
		virtual ~CFdmQuantoHelper ();
		!CFdmQuantoHelper ();

	internal:
		QuantLib::FdmQuantoHelper& GetReference ();
		boost::shared_ptr<QuantLib::FdmQuantoHelper>& GetShared ();
		QuantLib::FdmQuantoHelper* GetPointer ();
        void SetFdmQuantoHelper (boost::shared_ptr<QuantLib::FdmQuantoHelper> native)
        {
            if (_ppFdmQuantoHelper != NULL)
                delete _ppFdmQuantoHelper;
            _ppFdmQuantoHelper = new boost::shared_ptr<QuantLib::FdmQuantoHelper> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::FdmQuantoHelper>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Double QuantoAdjustment (Double equityVol, Double t1, Double t2) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CFdmQuantoHelper_Factory : public System::MarshalByRefObject,  public IFdmQuantoHelper_Factory
	{
	public:
        virtual IFdmQuantoHelper^ Create (Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ fTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ fxVolTS, Double equityFxCorrelation, Double exchRateATMlevel);
    };
   
/*Cephei*/ } /*QL*/ } /*Experimental*/ } /*Finitedifferences */}
